<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标题">
          <a-input v-decorator="[ 'title',validatorRules.title]" placeholder="请输入标题" />
        </a-form-item>
        <a-form-item label="海报宣传类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <cn-dict-select-tag
            placeholder="请选择"
            dict-code="factory_type"
            @change="dotype"
            :triggerChange="true"
            :options="{value:'dictDataCode',label:'dictDataDesc'}"
            v-decorator.trim="[ 'dataType', validatorRules.dataType]"/>
        </a-form-item>
        <a-form-item label="用户类型" :labelCol="labelCol" :wrapperCol="wrapperCol" v-if="selecttype == 'poster_pic'">
          <cn-dict-select-tag
            placeholder="请选择"
            dict-code="user_type"
            triggerChange
            :options="{value:'dictDataCode',label:'dictDataDesc'}"
            v-decorator.trim="[ 'userType', validatorRules.userType]"/>
        </a-form-item>
        <a-form-item label="资料类型" :labelCol="labelCol" :wrapperCol="wrapperCol" v-if="selecttype == 'material_pic'">
          <cn-dict-select-tag
            placeholder="请选择"
            dict-code="material_type"
            triggerChange
            :options="{value:'dictDataCode',label:'dictDataDesc'}"
            v-decorator.trim="[ 'materialType', validatorRules.materialType]"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="发布组织" >
          <j-tree-select placeholder="请选择" v-decorator="['orgCode', {}]" data-type="org" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="发布用户">
          <j-select-user-by-dep placeholder="请选择被指定人" v-decorator="['appointorCode', {}]" :trigger-change="true"></j-select-user-by-dep>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="开始时间">
          <a-date-picker format="YYYY-MM-DD" valueFormat="YYYY-MM-DD" v-decorator="['startTime', validatorRules.startTime]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="结束时间">
          <a-date-picker format="YYYY-MM-DD" valueFormat="YYYY-MM-DD" v-decorator="[ 'endTime',validatorRules.endTime]"/>
        </a-form-item>

        <a-form-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-image-upload class="avatar-uploader" :isMultiple="isMultiple" text="上传" v-decorator="[ 'urls', validatorRules.urls]"></j-image-upload>
        </a-form-item>


        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="内容">
          <a-textarea placeholder="请输入内容" maxlength='100' v-decorator="['remarks', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否启用/停用">
          <a-radio-group v-decorator="['status', validatorRules.status]" placeholder="是否启用/停用">
            <a-radio value="009" aria-selected="true">
              启用
            </a-radio>
            <a-radio value="003">
              停用
            </a-radio>
          </a-radio-group>
        </a-form-item>


      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction,getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import AFormItem from 'ant-design-vue/es/form/FormItem'
  import ATextarea from 'ant-design-vue/es/input/TextArea'
  import JImageUpload from '@/components/cn-form/JImageUpload'
  import JTreeSelect from '@/components/cn-form/JTreeSelect'
  import JSelectUserByDep from '@/components/cn-select-popup/JSelectUserByDep'
  export default {
    name: 'SfaPicFactoryModal',
    components: {
      JImageUpload,
      AFormItem,
      ATextarea,
      JTreeSelect,
      JSelectUserByDep
    },
    data () {
      return {
        isMultiple: true,
        title:"操作",
        visible: false,
        selecttype:'',
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          materialType:{ rules: [{
              required: true, message: '请选择资料类型!'
            }]},
          dataType:{ rules: [{
              required: true, message: '请选择类型!'
            }]},
          userType: {
            rules: [{
              required: true, message: '请选择用户类型!'
            }]
          },
          startTime: {
            rules: [{
              required: true, message: '请选择开始时间!'
            }]
          },
          endTime: {
            rules: [{
              required: true, message: '请选择结束时间!'
            }]
          },
          urls: {
            rules: [{
              required: true, message: '请上传海报!'
            }]
          },
          title: {
            rules: [{
              pattern: /^[\u4e00-\u9fa5_a-zA-Z0-9]{4,64}$/,
              required: true, message: '请输入4~64位内容为中文，英文字母和数字及下划线的字符!'
            }]
          },
          status: {
            rules: [{
              required: true, message: '请选择状态!'
            }]
          }
        },
        url: {
          add: window._CONFIG['domianSfaURL']+ "/picfactory/saveOrUpdate",
          info: window._CONFIG['domianSfaURL']+ "/picfactory/getInfoById",
        },
      }
    },
    methods: {
      dotype(val){
           this.selecttype = val
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.model.isRoll = this.model.isRoll+"";
        this.model.isControl = this.model.isControl+"";
        this.getImgUrls(record)
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'remarks','startTime','endTime',
            'status', 'title', 'userType','appointorCode','orgCode','orgName','dataType','materialType'))
        });
      },
      getImgUrls(record){
        var that = this
        let formData = Object.assign({}, record);
        getAction(this.url.info,formData,'get').then((res)=>{
          if(res.success){
            that.model.urls = res.result
            this.$nextTick(() => {
              this.form.setFieldsValue(pick(that.model,'urls'))
            });
          }else{
          }
        }).finally(() => {

        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            httpurl += this.url.add;
            let method = '';
            method = 'post';
            let formData = Object.assign(this.model, values);
            //时间格式化
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
      onChange(value){
        if(!value){
          this.treeValue = ''
          this.form.typeCode = ''
        } else {
          this.treeValue = value
          this.form.typeCode = value
        }
      },
    }
  }
</script>

<style scoped>

</style>